<?php

namespace common\models\shop;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%shop_attachment}}".
 *
 * @property int $id
 * @property int $product_id 货品ID
 * @property int $goods_id 商品ID
 * @property string $src 附件地址
 * @property string $title 附件标题
 * @property int $format 格式：0图片，1视频
 * @property int $listorder 显示排序
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class ShopAttachment extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%shop_attachment}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['product_id', 'goods_id', 'format', 'listorder', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['src'], 'required'],
            [['src'], 'string', 'max' => 255],
            [['title'], 'string', 'max' => 50],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'product_id' => '货品ID',
            'goods_id' => '商品ID',
            'src' => '附件地址',
            'title' => '附件标题',
            'format' => '格式：0图片，1视频',
            'listorder' => '显示排序',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    public function fields() {
        return [
            'id',
            'format' => function() {
                return $this->format ? 'video' : 'image';
            },
            'path' => function() {
                return Url::attach($this->src);
            },
            'listorder',
        ];
    }

}
